/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { shallowReactive } from 'vue';
import {
  BgLayerController,
  GlobalConfigController,
  RenderLayerController,
} from '../controller';
import { UICoordinateController } from '../controller/ui-coordinate.controller';

/**
 * @description 全局数据
 */
const Store: Record<string, any> = {};

/**
 * @description 初始化状态数据
 */
export const initStore = (props: any, emit: any): void => {
  const rootEmit = emit;
  Store.$rootEmit = rootEmit;
  const config = new GlobalConfigController(props);
  Store.$config = shallowReactive(config);
  const bgLayer = new BgLayerController(config, rootEmit);
  Store.$bgLayer = shallowReactive(bgLayer);
  const renderLayer = new RenderLayerController(
    config,
    rootEmit,
    props.resources,
    props.tasks,
  );
  Store.$renderLayer = shallowReactive(renderLayer);
  const uiCoordinate = new UICoordinateController(config);
  Store.$uiCoordinate = shallowReactive(uiCoordinate);
};

/**
 * @description 状态hook
 */
export const useStore = (): {
  $config: GlobalConfigController;
  $bgLayer: BgLayerController;
  $renderLayer: RenderLayerController;
  $uiCoordinate: UICoordinateController;
  $rootEmit: (event: string, ...args: any[]) => void;
} => {
  return {
    // 配置
    $config: Store.$config,
    // 背景层
    $bgLayer: Store.$bgLayer,
    // 渲染层
    $renderLayer: Store.$renderLayer,
    // 坐标层
    $uiCoordinate: Store.$uiCoordinate,
    // 根emit
    $rootEmit: Store.$rootEmit,
  };
};

export default useStore;
